package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcPhoneDataMapper;
import com.csun.cmny.provider.device.phone.entity.PhoneDataEntity;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcPhoneData;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcPhoneDataService;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-09 16:51
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcPhoneDataServiceImpl extends BaseService<DmcPhoneData> implements DmcPhoneDataService {

    @Resource
    private DmcPhoneDataMapper dmcPhoneDataMapper;

    @Resource
    private DmcDeviceService dmcDeviceService;

    /**
     * query real phone data by device id
     *
     * @param deviceId the device id
     *
     * @return the dmc phone data
     */
    @Override
    public DmcPhoneData queryRealPhoneDataByDeviceId(Long deviceId) {

        Preconditions.checkArgument(deviceId != null, "设备Id不能为空");

        DmcDevice dmcDeviceQuery = new DmcDevice();
        dmcDeviceQuery = dmcDeviceService.selectByKey(deviceId);
        if (PublicUtil.isEmpty(dmcDeviceQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610002, deviceId);
        }

        return dmcPhoneDataMapper.selectByDeviceId(deviceId);
    }

    /**
     * update phone data by device id
     *
     * @param dmcPhoneData dmc phone data
     *
     * @return the int
     */
    @Override
    public Integer updatePhoneDataByDeviceId(DmcPhoneData dmcPhoneData) {

        return dmcPhoneDataMapper.updateByPrimaryKeySelective(dmcPhoneData);
        //return update(dmcPhoneData);
    }

    /**
     * delete phone data by device id
     *
     * @param deviceId device id
     *
     * @return the int
     */
    @Override
    public Integer deletePhoneDataByDeviceId(Long deviceId) {

        Example example = new Example(DmcPhoneData.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("deviceId", deviceId);

        return dmcPhoneDataMapper.deleteByExample(example);
    }

    /**
     * 查询全部的电话机数据
     *
     * @return
     */
    @Override
    public List<PhoneDataEntity> queryPhoneDataBindList() {

        return dmcPhoneDataMapper.queryPhoneDataBindList();
    }

}
